import {  safeToNumber } from '@/utils/LodashUtils.js'
import _ from 'lodash'
import { usePriceSummaryStore } from '@/stores/index.js'
import { ingredientListSuper } from '@/promises/ingredientListFn.js'

const priceSummaryStore = usePriceSummaryStore()
const { getVal } = priceSummaryStore

export const defineIngredientListRFn = (data) => {
  data.ingredientListR = () => ingredientListSuper(data)
}

export const setData = async (data) => {
  data.raw = data.raw.filter(item => getVal(item, 'contract') === data.contract)
  let result = {}
  const getFn = (key, data) => {
    switch (key) {
      case 'factory': {
        result = getVal(data.raw[0], key)
        break
      }
      case 'ingredients': {
        const groupByResult = _.groupBy(data.raw, item => getVal(item, 'weft') + getVal(item,'ingredient'))
        if (_.isEmpty(groupByResult)) {
          result = []
          break
        }
        let idx = 0
        const totalWeight = _.reduce(data.raw, (prev, cur) => prev + safeToNumber(getVal(cur, 'quantity')), 0)
        if (totalWeight) {
          const ingredients = _.map(groupByResult, (v, k) => {
            //OK办寄出日期为退纱不需要统计!
            v = v.filter(item => getVal(item, 'deliveryDay') !== '退纱')
            const weight = v.reduce((prev, cur) => prev + safeToNumber(getVal(cur, 'quantity')), 0)
            if (weight) {
              const total = v.reduce((prev, cur) => prev + safeToNumber(getVal(cur, 'weftCost')) + safeToNumber(getVal(cur, 'deliveryCost')), 0)
              idx++
              return {
                idx,
                ingredient: k,
                maker: getVal(v[0], 'weftFactory'),
                weight,
                percentage: safeToNumber(_.divide(weight, totalWeight)),
                price: safeToNumber(_.divide(total, weight))
              }
            }
          })
          // console.log(ingredients)
          result = ingredients
        } else {
          result = '总重量为0, 请检查已完成总表数据'
        }
        break
      }
      case 'ingredientAvg': {
        const totalWeight = _.reduce(data.raw, (prev, cur) => prev + safeToNumber(getVal(cur, 'quantity')), 0)
        if (totalWeight) {
          const totalPrice = _.reduce(data.raw, (prev, cur) => prev + safeToNumber(getVal(cur, 'weftCost')) + safeToNumber(getVal(cur, 'deliveryCost')), 0)
          result = _.divide(totalPrice, totalWeight)
        } else {
          result = '总重量为0, 请检查已完成总表数据'
        }
        break
      }
      case 'model': {
        result = Array.from(new Set(data.raw.map(item => getVal(item, 'model')))).join(',')
        break
      }
      case 'client': {
        result = getVal(data.raw[0], 'client')
        break
      }
    }
    data[key] = result
  }

  getFn('factory', data)
  getFn('ingredients', data)
  getFn('ingredientAvg', data)
  getFn('model', data)
  getFn('client', data)

  return data

  // data.ingredients = [
  //   { ingredient: '2/75D 涤纶丝', maker: '柯珞克1', total: '10000' },
  //   { ingredient: '2/16S 60棉/40晴膨体', maker: '柯珞克2', total: '20000' },
  //   { ingredient: '1.8倍 氨纶', maker: '柯珞克3', total: '30000' },
  //   { ingredient: '1/140D 涤纶丝', maker: '柯珞克4', total: '40000' }
  // ]
}
